package com.garen.fire.dao.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Repository;

import com.garen.common.BaseDao;
import com.garen.fire.dao.IFireCarDao;
import com.garen.fire.entity.FireCar;
import com.garen.utils.MapBuilder;


@Repository
public class FireCarDaoImpl extends BaseDao<FireCar> implements IFireCarDao {

	@PostConstruct
	private void init(){
		initOrm(FireCar.class);
	}

	/*
	 * 通过UUID统计车辆类型数量
	 * */
	public List<Map<String, Object>> findFireCarByUUID(Set<String> carUUIDs) {
		String sql = " SELECT t2.code_name as carTypeName,count(1) carCount from fire_car t1 ,fire_code t2 " + 
				" where t2.code_type = '3' and t1.car_type = t2.code_value and  t1.car_uuid in (:uuids) " + 
				" GROUP BY t1.car_type " ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("uuids", carUUIDs)
				 .build();
		return queryForListMap(sql,paramMap);
	}
	/*
	 * 通过案件编号统计车辆类型数量
	 * */
	@Override
	public List<Map<String, Object>> findFireCarByAjbh(String ajbh) {
		String sql = " select tt1.code_name  as carTypeName,count(1) as carCount \n" + 
				" from (select  DISTINCT t1.car_id,t1.car_type,t2.code_name from fire_car t1 ,fire_code t2,fire_case_record t3\n" + 
				" where t2.code_type = '3' and t1.car_type = t2.code_value \n" + 
				" and t1.car_id = t3.fire_car and t3.fire_ajbh = :ajbh) as tt1\n" + 
				" GROUP BY tt1.car_type " ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("ajbh", ajbh)
				 .build();
		return queryForListMap(sql,paramMap);
	}
	
	//获取中队的正常状态的车,信息
	@Override
	public List<FireCar> getFireCarList(String orgId){
		String sql = "select t.car_status,t.car_num,t.car_id ,ifnull(t.car_man_count,8) as car_man_count,"
				+ "ifnull(t.car_capacity,0) as car_capacity,"
				+ "(select tt.code_name  from fire_code tt "
				+ " where tt.code_value= t.car_type and tt.code_type = '3' limit 1) as car_type_name," 
				+ "t.car_uuid from fire_car t  where t.car_status = '1' and t.car_org_id = :orgId ";
		Map<String,Object> paramMap = new MapBuilder()
			 .put("orgId", orgId)
			 .build();
		return getListBySQL(sql, paramMap);
	}
	@Override
	public List<Map<String, Object>> getcarcount(String org_id) {
		// TODO Auto-generated method stub
		String sql = "SELECT(select count( distinct t1.fire_car) from fire_case_record t1 , fire_case_info t2 where t1.fire_ajbh = t2.ajbh and t1.fire_org=:orgId and t2.status <> 8  and to_days(t1.fire_date) = to_days(now()))  AS zzcarcount,(select count(1) from fire_car where car_org_id=:orgId and car_status=1)as carcount,(select count(1) from fire_car where car_org_id=:orgId and car_status=2) as tycar" ;
		Map<String,Object> paramMap = new MapBuilder()
				 .put("orgId",org_id)
				 .build();
		List<Map<String,Object>> list=queryForListMap(sql,paramMap);
		return list;
	}

	@Override
	public List<FireCar> getAllFireCarListByOrgId(Integer orgId) {
		// TODO Auto-generated method stub
		String sql = "SELECT c.car_uuid,c.car_alias,c.car_num,c.car_org_id,c.car_type,c.device_id,c.bz,"
				+ " ifnull(c.car_man_count, 8) AS car_man_count,ifnull(c.car_capacity, 0)  AS car_capacity,"
				+ " (CASE WHEN exists(SELECT * FROM (SELECT * FROM fire_case_info WHERE bjsj > subdate(now(), 1) AND status <> 8 AND status <> 0) i, fire_case_record r WHERE i.ajbh = r.fire_ajbh AND r.fire_car = c.car_id) THEN 10 ELSE c.car_status END) AS car_status,"
				+ " (SELECT tt.code_name FROM fire_code tt WHERE tt.code_value = c.car_type AND tt.code_type = '3' LIMIT 1) AS car_type_name"
				+ " FROM fire_car c where c.device_id and c.car_org_id = :orgId";
		Map<String, Object> paramMap = new MapBuilder().put("orgId", orgId).build();
		return getListBySQL(sql, paramMap);
	}

	@Override
	public List<FireCar> getAllFireCarListByOrgId(List<Integer> orgIdList) {
		// TODO Auto-generated method stub
		String sql = "SELECT c.car_uuid,c.car_alias,c.car_num,c.car_org_id,c.car_type,c.device_id,c.bz,"
				+ " ifnull(c.car_man_count, 8) AS car_man_count,ifnull(c.car_capacity, 0)  AS car_capacity,"
				+ " (CASE WHEN exists(SELECT * FROM (SELECT * FROM fire_case_info WHERE bjsj > subdate(now(), 1) AND status <> 8 AND status <> 0) i, fire_case_record r WHERE i.ajbh = r.fire_ajbh AND r.fire_car = c.car_id) THEN 10 ELSE c.car_status END) AS car_status,"
				+ " (SELECT tt.code_name FROM fire_code tt WHERE tt.code_value = c.car_type AND tt.code_type = '3' LIMIT 1) AS car_type_name"
				+ " FROM fire_car c where c.device_id and c.car_org_id in (:orgIdList) ";
		Map<String, Object> paramMap = new MapBuilder().put("orgIdList", orgIdList).build();
		return getListBySQL(sql, paramMap);
	}
	
	
	
	
	
}
